package com.droidathletics.boundary.microcycles;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.droidathletics.R;
import com.droidathletics.boundary.MainMenu;
import com.droidathletics.control.ManageMicrocycle;
import com.droidathletics.entity.Macrocycle;
import com.droidathletics.entity.Microcycle;

public class ListMicrocycles extends Activity {

    
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_microcycles);
        
        instance=this;
        
        Bundle b = getIntent().getExtras();
        int position = b.getInt("key");
        positionMacro=position;
        final Macrocycle editedMacrocycle = (Macrocycle) MainMenu.listMacrocycles.getAdapter().getItem(position);
        
        manageMicrocycle = MainMenu.manageMicrocycle;
        
        listMicrocycles = (ListView) findViewById(R.id.listMicrocycles);
 		adapterMicrocycle= new ImageAdapterListMicrocycles(this, 
 				manageMicrocycle.readAllMicrocycles(editedMacrocycle.getName()));
 		listMicrocycles.setAdapter(adapterMicrocycle);

 		listMicrocycles.setOnItemClickListener(new OnItemClickListener() {
 		    public void onItemClick(AdapterView<?> a, View v, final int position, long id) { 		    	
 		    	final Microcycle itemMicrocycle = (Microcycle) listMicrocycles.getAdapter().getItem(positionMacro);
				if(itemMicrocycle != null) {
 		            AlertDialog.Builder builder = new AlertDialog.Builder(instance);
 		            builder.setTitle("Pick an Option");
 		            builder.setItems(popupItems, new DialogInterface.OnClickListener() {
 		                public void onClick(DialogInterface dialog, int item) {
 		                	switch(item){
	 		                	case 0:
	 		                		manageMicrocycle.deleteMicrocycle(itemMicrocycle.getNumberOfWeek(),
	 		                				itemMicrocycle.getMacrocycleName());
	 		                		adapterMicrocycle= new ImageAdapterListMicrocycles(instance, 
	 		                						manageMicrocycle.readAllMicrocycles(editedMacrocycle.getName()));
	 		                		listMicrocycles.setAdapter(adapterMicrocycle);
	 		                		return;
	 		 	 		          case 1:

	 		                		finish();
	 		                		Intent intent = new Intent(ListMicrocycles.this, EditMicrocycles.class);
		 		               		Bundle b = new Bundle();
		 		               		b.putInt("key", positionMacro); //Your id
		 		               		b.putInt("key1", position);
		 		               		intent.putExtras(b); //Put your id to your next Intent		 		               		
		 		                   	startActivity(intent);
		 		   	            	return;
 		                	}
 		                }
 		            });
 		            AlertDialog alert = builder.create();
					alert.show();
				}
 		    }
 		});
	}
	
	@Override
    public void onBackPressed() {
		finish();
        Intent intent = new Intent(ListMicrocycles.this, MainMenu.class);
        startActivity(intent);
	    return;
    }
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu_options, menu);
        return true;
    }
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.add_option:
            addItem();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
	
	private void addItem(){
		finish();
 		Intent intent = new Intent(ListMicrocycles.this, CreateMicrocycles.class);
		Bundle b = new Bundle();
		b.putInt("key", positionMacro); //Your id
		intent.putExtras(b); //Put your id to your next Intent
    	startActivity(intent);
	}
	private int positionMacro;
	public static ListView listMicrocycles;
	private ManageMicrocycle manageMicrocycle;
    public static ImageAdapterListMicrocycles adapterMicrocycle;
    public static ListMicrocycles instance; 
    private final CharSequence[] popupItems = {"Delete", "Edit", "Cancel"};
}
